<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="hardcode2013.*" %>
<%@ page import="java.util.List" %>

<html>
	<head>
		<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
		
		<script type="text/javascript">
		function floatInputOnChange(input) {
			if (! input.value.match("^[0-9]*(\.[0-9]{1,2})?$")) input.value = "";
		}
		function floatInputOnKeyPress(evt) {
			var charCode = (evt.which) ? evt.which : event.keyCode;
			if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
				return false;
			else
			return true;
		}
		function selectAllOnChange(checkbox) {
			var selectCheckboxes = document.getElementsByName("select_checkbox");
			for (var i = 0; i < selectCheckboxes.length; i++) {
				selectCheckboxes[i].checked = checkbox.checked;
			}
		}
		function itemAction(selValue, keyValue) {
			if (selValue != null) {
				document.getElementById("action").value = "single_item_action"; 
				document.getElementById("single_item_action").value = selValue;
				document.getElementById("single_item_key").value = keyValue;
				document.getElementById("item_form").submit();   
			}
		}
		</script>
	</head>
	
	<body>
	
	<%@include file="include_header.jsp" %>
  
	<%
	final String currentUrl = "/myitems.jsp";
	
  	if (profile != null) {
  		
		String pageStr = request.getParameter("page");
		int currentPage = 1;
		if (pageStr != null) {
			try {
				currentPage = Integer.parseInt(pageStr);
			} catch (Exception e) {
			}
		}

		if (currentPage <= 0) {response.sendRedirect(currentUrl); return;}
		ItemServ.ItemQueryResult itemRes = ItemServ.getUserItems(profile.getUsername(), currentPage, (profile != null && profile.getIsAdmin()), (profile != null ? profile.getUsername() : null));
		int totalPages = itemRes.totalItems / ItemServ.resultsPerPage + (itemRes.totalItems % ItemServ.resultsPerPage == 0 ? 0 : 1);
		if (currentPage > totalPages && itemRes.totalItems > 0) {response.sendRedirect(currentUrl + "?page=" + totalPages); return;}
		
		String pageLbl = "Total Items: " + itemRes.totalItems;
		if (itemRes.totalItems > 0)
			pageLbl += ", " +
				"Showing: " + ((currentPage - 1) * ItemServ.resultsPerPage + 1) + "-" + (currentPage * ItemServ.resultsPerPage > itemRes.totalItems ? itemRes.totalItems : currentPage * ItemServ.resultsPerPage) + ", " +
				"Page: " + currentPage + " / " + totalPages +
				(currentPage > 1 ? " <a href=\"" + EncodeUtil.encodeHtml(currentUrl) + "?page=" + (currentPage - 1) + "\">Prev</a>" : "") +
				(currentPage < totalPages ? " <a href=\"" + EncodeUtil.encodeHtml(currentUrl) + "?page=" + (currentPage + 1) + "\">Next</a>" : "");
	%>
	
	<fieldset>
		<legend>My Items</legend>
		
		<a href="/create_item.jsp">Create Item</a><br>
		
		<%= pageLbl %>
		
	    <form id="item_form" action="/item" method="post">
			<table border="1" width="100%">
				<tr>
					<td><input type="checkbox" onchange="selectAllOnChange(this)"></td>
					<td>Title</td>
					<td>Description</td>
					<td>Price</td>
					<td>Creation Date</td>
					<td>Expiration Date</td>
					<td>Is Expired</td>
					<td>Action</td>
				</tr>
				<%
				for (ItemProfile i : itemRes.itemList) {
					%>
					<tr>
						<td><input type="checkbox" name="select_checkbox" value=<%= EncodeUtil.encodeHtml(i.getKeyStr()) %>></td>
						<td><%=EncodeUtil.encodeHtml(i.getTitle())%></td>
						<td><%=EncodeUtil.encodeHtml(i.getDescription()).replaceAll("\n", "<br>")%></td>
						<td>$<%=EncodeUtil.encodeHtml(i.getPrice())%></td>
						<td><%=EncodeUtil.encodeHtml(i.getCreationDateTime())%></td>
						<td><%=EncodeUtil.encodeHtml(i.getExpirationDateTime())%></td>
						<td><%=String.valueOf(i.isExpired()).replace("true","Yes").replace("false","No")%></td>
						<td>
							<select onchange="itemAction(this.options[this.selectedIndex].value, <%= EncodeUtil.encodeHtml("\"" + i.getKeyStr() + "\"") %>)">
								<option value=null>Select Action</option>
								<option value="edit">Edit</option>
								<option value="delete">Delete</option>
							</select>
						</td>
					</tr>
					<%
				}
				%>
			</table>
			
			<select name="multi_item_action">
				<option value=null>Select Action</option>
				<option value="delete">Delete</option>
			</select>
			<input type="hidden" id="single_item_action" name="single_item_action" value=null>
			<input type="hidden" id="single_item_key" name="single_item_key" value=null>
			<input type="hidden" id="action" name="action" value="multi_item_action">
			<input type="hidden" name="redirect" value="<%= "/myitems.jsp?page=" + String.valueOf(currentPage) %>">
			<input type="hidden" name="auth_key" value="<%= SessionUtil.getAuthKey(request) %>">
			<input type="submit" value="Perform" />
		</form>
	</fieldset>
		<%
	}
	%>
	</body>
</html>